/**
 * 下载文件
 * @param url 文件下载地址
 */
function downloadFile(url,postdata){
    // 获取url和data
    if( url != ""){ 
    	if(postdata != undefined){
	        // data 是 string 或者 array/object
	    	postdata = typeof postdata == 'string' ? postdata : jQuery.param(postdata);
	        // 把参数组装成 form的  input
	        var inputs = '';
	        
	        jQuery.each(postdata.split('&'), function(){ 
	            var pair = this.split('=');
	            inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; 
	        });
	        
	     // request发送请求
	        jQuery('<form action="'+ url +'" method="post">' + inputs + '</form>')
	        .appendTo('body').submit().remove();
    	}else{
	        // request发送请求
	        jQuery('<form action="'+ url +'" method="post"></form>')
	        .appendTo('body').submit().remove();
    	}
    }
}

/**
 * 将数组转换为字符串，用“，”隔开
 * 
 * @param values
 * @returns {String}
 */
function array2Str(values){
	if(values == null){
		return "";
	}
	
	var ids = "";
	for(var i=0;i<values.length;i++){
        if(i< (values.length-1)){
        	ids = ids + values[i] + ","; 
        }else{
        	ids = ids + values[i];
        }
	}
	return ids;
}

/**
 * 判断是否IE
 */
function isIE(){
	 if (window.ActiveXObject){
		 return true;
	 }else{
		 return false;
	 }
	 
}

/**
 * 实数验证
 */
function isFloat(obj) {         
    if (/^(-|\+)?\d+(\.\d+)?$/.test(obj)){  
         return true;  
      }else{  
        return false;  
    }  
}  

/**
 * 整数验证
 * @param value
 * @returns {Boolean}
 */
function isInt(value){
    var pattern = /^-?\d+$/;
    if(value.search(pattern)!=0){
        return false;
    }
    return true;
}

JSON.stringify = JSON.stringify || function (obj) {
    var t = typeof (obj);
    if (t != "object" || obj == null) {
        // simple data type
        if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    }
    else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n]; t = typeof(v);
            if (t == "string") v = '"'+v+'"';
            else if (t == "object" && v !== null) v = JSON.stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
};

//数字四舍五入
Number.prototype.toFixed = function(d){
    var s = this + "";
    if(!d)
    	d = 0;
    if(s.indexOf(".")==-1)
    	s += ".";
    
    s += new Array(d+1).join("0");

    if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+ (d+1) +"})?)\\d*$").test(s)){
        var s = "0"+ RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true;

        if (a == d+2){
        	a = s.match(/\d/g); 
        	if (parseInt(a[a.length-1])>4){
        		for(var i = a.length-2; i>=0; i--) {
        			a[i] = parseInt(a[i])+1;
        			if(a[i] == 10){
        				a[i]=0; 
        				b = i != 1;
        			} else break;
        		}
        	}

        	s = a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");
        }
        
        if(b)
        	s = s.substr(1);
        return (pm + s).replace(/\.$/, "");
    } 
    
    return this + "";
};  

/**
 * 文字转大写
 * @param array
 */
function textToUpper(array){
	for(var i=0;i<array.length;i++){
		var $textObj = array[i];
		$textObj.focus(function(){
			$textObj.bind("keyup", function(){
				$textObj.val($textObj.val().toUpperCase());
			  });
			$textObj.bind("paste", function(){
				$('body').mousemove(function(){
					$textObj.val($textObj.val().toUpperCase());
				});
	          });
		}); 
	}
};

/**
 * 文本去空格
 */
String.prototype.trim = function(){   
    return this.replace(/(^\s*)|(\s*$)/g, "");   
};

/**
 * 判断非空文本
 * @param str
 * @returns {Boolean}
 */
function isNotEmptyStr(str){
	if(str == null || str.trim().length == 0){
		return false;
	}
	
    return true;  
};

/**
 * 判断空文本
 * @param str
 * @returns {Boolean}
 */
function isEmptyStr(str){
	if(str == null || str.trim().length == 0){
		return true;
	}
	
    return false;  
};

